package com.dure.hotmemory.utils;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.dure.commonlib.util.PreferencesUtils;
import com.dure.hotmemory.listeners.AdPresentListener;
import com.socks.library.KLog;

/**
 * Created by longzhang on 7/19/16.
 */
public class ADUtil {

    public static void showBannerAd(Activity context, RelativeLayout view){
        String adType = PreferencesUtils.getString(context, "ad_type", "gdt");
        KLog.d("lesleixz", "banner ad_type = "+ adType);
        if (TextUtils.equals(adType, "baidu")){
//            BaiduADUtil.showBannerView(context, view);
            GDTADUtil.showBannerView(context, view);
        }else if (TextUtils.equals(adType, "gdt")){
            KLog.d("lesleixz", "show gdt ad");
//            BaiduADUtil.showBannerView(context, view);
            GDTADUtil.showBannerView(context, view);
        }
    }

    public static void showSplashAd(Activity context, ViewGroup view, AdPresentListener adPresentListener){
        String adType = PreferencesUtils.getString(context, "ad_type");
        adType = "baidu";
        KLog.d("lesleixz", "splash ad_type = "+ adType);
        if (TextUtils.equals(adType, "baidu")){
            BaiduADUtil.showSplashAD(context, view);
        }else if (TextUtils.equals(adType, "gdt")){

//            GDTADUtil.showSplashAd(context, view, adPresentListener);
        }
    }

    public static void showInterstitialAd(Activity context){
        String adType = PreferencesUtils.getString(context, "ad_type");
        KLog.d("lesleixz", "splash ad_type = "+ adType);
        if (TextUtils.equals(adType, "baidu")){

        }else if (TextUtils.equals(adType, "gdt")){
            GDTADUtil.showInterstitialAd(context);
        }
    }

    public static void destroyBannerView(Activity context){
        String adType = PreferencesUtils.getString(context, "ad_type");
        if (TextUtils.equals(adType, "baidu")){
            BaiduADUtil.destroyBannerAD();
        }else if (TextUtils.equals(adType, "gdt")){
            GDTADUtil.destroyBannerAd();
        }
    }

    /**
     * 当设置开屏可点击时，需要等待跳转页面关闭后，再切换至您的主窗口。故此时需要增加canJumpImmediately判断。 另外，点击开屏还需要在onResume中调用jumpWhenCanClick接口。
     */
    public static boolean canJumpImmediately = false;

    public static void jumpWhenCanClick(Activity activity) {
        KLog.d("lesleixz", "this.hasWindowFocus():" + activity.hasWindowFocus());
        if (canJumpImmediately) {
//            activity.startActivity(new Intent(activity, MainActivity.class));
            activity.finish();
        } else {
            canJumpImmediately = true;
        }

    }

    /**
     * 不可点击的开屏，使用该jump方法，而不是用jumpWhenCanClick
     */
    public static void jump(Activity activity) {
//        activity.startActivity(new Intent(activity, MainActivity.class));
        activity.finish();
    }
}
